home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2434 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: quick decision: is n a power of 2?
  5. Date: Sun, 21 Jan 1996 12:12:28 GMT
  6. Organization: Netcom
  7. Message-ID: <31022d1e.215811776@nntp.ix.netcom.com>
  8. References: <4dpd94$c25@fountain.mindlink.net> <4dsaco$a39@newsbf02.news.aol.com> <9601211103.AA19325@dxmint.cern.ch>
  9. NNTP-Posting-Host: ix-dc10-02.ix.netcom.com
  10. X-NETCOM-Date: Sun Jan 21  4:12:15 AM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. Dan Pop <danpop@mail.cern.ch> wrote:
  14.  
  15. > babycox@aol.com (BabyCox) writes:
  16. > >Here is a revision, this should work (but that's what I said last time)
  17. > You're a little bit closer this time :-)
  18. > >Boolean isPowerOfTwo(long x)
  19. > >{
  20. > >  long tst;
  21. > >  for(tst=1<<31;tst!=0;tst>>=1)
  22. > >    if (x==tst) return true;
  23. > >  
  24. > >  return false;
  25. > >}
  26. > I've already commented about the usage of the undefined Boolean, true and
  27. > false.
  28. > This new version will work _only_ on some implementations.  To make it
  29. > portable, tst has to be unsigned long (x should also be unsigned long
  30. > because negative numbers are _never_ powers of two :-)
  31. > If your C book hasn't taught you that the behaviour of the >> operator is
  32. > implementation-defined when the first argument is negative, then it's time
  33. > to read K&R2.
  34.  
  35. There's also the problem that it will fail if long is more than 32
  36. bits and x is greater than pow(2, 32).
  37.  
  38.  
  39. Michael M Rubenstein
  40.